//index.js
const app = getApp()
Page({
  data: {
    userName: app.getGlobalUserInfo().nickName,
    activeNames: ['0'],
    //九宫格内容
    gridAppLet: [
      { title: "英语四级", icon: "smile-comment-o", gridClick: "CET" },
      { title: "Vant Weapp", icon: "hot-o", gridClick: "VantWeApp" },
      { title: "黑马程序员", icon: "video-o", gridClick: "dark_horse" },
    ],
    //推荐的内容
    cardImg: [
      {
        title: "考试酷",
        desc: "专业题目练习(点击出现复制)",
        imageURL: "https://img.examcoo.com/1458/themes/metro/images/v1_3e.jpg",
        link: "https://examcoo.com/index/ku",
        isHasButton:true,
        buttonTap:"cardCopyLink",
        bottom_text:"复制"
      },
      {
        title: "TaroUI",
        desc: "小程序组件库",
        imageURL: "https://taro-ui.jd.com/favicons/favicon.png",
        link: "wx1c6850423c0ff174",
        isHasButton:true,
        buttonTap:"appLetLink",
        bottom_text:"打开"
      },
    ]
  },
  /**
   * 九宫格点击事件
   */
  CET() {
    if (this.data.userName != null) {
      wx.navigateToMiniProgram({
        appId: 'wx7876b4c2ba91c5e8',
      })
    } else {
      wx.showToast({
        icon: 'error',
        title: '你尚未登录',
        duration: 2000
      })
    }
  },
  VantWeApp() {
    if (this.data.userName != null) {
      wx.navigateToMiniProgram({
        appId: 'wx1c01b35002d3ba14',
      })
    } else {
      wx.showToast({
        icon: 'error',
        title: '你尚未登录',
        duration: 2000
      })
    }
  },
  dark_horse() {
    if (this.data.userName != null) {
      wx.navigateToMiniProgram({
        appId: 'wx94e37988531c8ab8',
      })
    } else {
      wx.showToast({
        icon: 'error',
        title: '你尚未登录',
        duration: 2000
      })
    }
  },

  /** 
   * 推荐列表的点击事件
  */
  collapseChange(event) {
    this.setData({
      activeNames: event.detail,
    });
  },
  cardCopyLink(linkEvent) {
    // console.log(linkEvent);
    var link = linkEvent.currentTarget.dataset.link;
    //console.log(this.data.userName)
    if (this.data.userName != null) {
      wx.setClipboardData({
        data: link,
        success(res) {
          wx.showToast({
            title: '复制成功',
          })
        },
        fail(res) {
          wx.showToast({
            icon: 'error',
            title: '复制失败',
          })
        }
      })
    } else {
      wx.showToast({
        icon: 'error',
        title: '你尚未登录',
        duration: 2000
      })
    }
  },
  appLetLink(linkEvent) {
    var link = linkEvent.currentTarget.dataset.link;
    if (this.data.userName != null) {
      wx.navigateToMiniProgram({
        appId: link,
      })
    } else {
      wx.showToast({
        icon: 'error',
        title: '你尚未登录',
        duration: 2000
      })
    }
  },
  /**
   * 生命周期函数--监听页面加载
   */
  onLoad: function (options) {

  },

  /**
   * 生命周期函数--监听页面初次渲染完成
   */
  onReady: function () {

  },

  /**
   * 生命周期函数--监听页面显示
   */
  onShow() {
    this.getTabBar().init();
    this.setData({
      userName: app.getGlobalUserInfo().nickName,
    })
  },

  /**
   * 生命周期函数--监听页面隐藏
   */
  onHide: function () {

  },

  /**
   * 生命周期函数--监听页面卸载
   */
  onUnload: function () {

  },

  /**
   * 页面相关事件处理函数--监听用户下拉动作
   */
  onPullDownRefresh: function () {

  },

  /**
   * 页面上拉触底事件的处理函数
   */
  onReachBottom: function () {

  },

  /**
   * 用户点击右上角分享
   */
  onShareAppMessage: function () {

  },
})